package com.delong.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.delong.dao.CompanyMapper;
import com.delong.dao.DaoHelper;
import com.delong.model.app.Company;

public class CompanyService {
	public List<Company> getListElementByName(String name, int pageno, int limit) {
		SqlSession session = null;
		List<Company> result = new ArrayList<Company>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CompanyMapper mapper = session.getMapper(CompanyMapper.class);
			result = mapper.selectCategoryByName(name, pageno, limit);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

		return result;

	}

	public List<Company> getAllList() {
		SqlSession session = null;
		List<Company> result = new ArrayList<Company>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CompanyMapper mapper = session.getMapper(CompanyMapper.class);
			result = mapper.selectALLCompany();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

		return result;

	}

	public void addElement(Company element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CompanyMapper mapper = session.getMapper(CompanyMapper.class);
			mapper.addElement(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void changeElement(Company element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CompanyMapper mapper = session.getMapper(CompanyMapper.class);
			mapper.changeCompany(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void changeStatus(int status, int id) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CompanyMapper mapper = session.getMapper(CompanyMapper.class);
			mapper.changeStatus(status, id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public Company getElement(int id) {
		SqlSession session = null;
		Company result = new Company();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CompanyMapper mapper = session.getMapper(CompanyMapper.class);
			result = mapper.selectCompanyById(id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

		return result;
	}

	public int getCountByName(String companyName) {
		SqlSession session = null;
		int result = 0;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			CompanyMapper mapper = session.getMapper(CompanyMapper.class);
			result = mapper.selectCount(companyName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

		return result;
	}
}
